﻿using Common.Models;
using Stylet;
using System.Linq;
using System;
using Common;
using System.Collections.Generic;
using System.Windows.Controls;
using MaterialDesignThemes.Wpf;
using Common.AJControls;
using Aspose.Cells.Charts;
using Common.Utility.AJ;
using System.Windows;
using Common.Utility;
using Common.Model;
using Common.EF.Controllers;
using Yitter.IdGenerator;

namespace AWSV2.ViewModels
{
    public class GoodsSpecViewModel : Screen
    {
        private IWindowManager _windowManager;

        private AJErrorInfoDialogParams _errorInfo;
        public AJErrorInfoDialogParams ErrorInfo
        {
            get => _errorInfo;
            set => SetAndNotify(ref _errorInfo, value);
        }

        private string _title;
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                SetAndNotify(ref _title, value);
            }

        }

        private bool _loading;
        public bool Loading
        {
            get
            {
                return _loading;
            }
            set
            {
                SetAndNotify(ref _loading, value);
            }

        }

        private BindableCollection<GoodsSpec> _rows;
        public BindableCollection<GoodsSpec> Rows
        {
            get
            {
                return _rows;
            }
            set
            {
                SetAndNotify(ref _rows, value);
            }

        }

        private Goods _goods;

        private GoodsSpec _current;
        public GoodsSpec Current
        {
            get => _current;
            set => SetAndNotify(ref _current, value);
        }

        public GoodsSpecViewModel(IWindowManager windowManager, Goods goods)
        {
            _goods = goods;
            ErrorInfo = new AJErrorInfoDialogParams();
            _windowManager = windowManager;
            Title = I18NService.Translate(LangKeys._0_规格配置, goods?.Name);
            Rows = new BindableCollection<GoodsSpec>(goods?.SpecList ?? new List<GoodsSpec>());

            //ExecuteGetListCmd(null);
        }

        private bool _canCommit;
        public bool CanCommit
        {
            get => Rows?.Count > 0;
            set => SetAndNotify(ref _canCommit, value);
        }
        public async void Commit()
        {
            Loading = true;
            try
            {
                using var ctrl = new GoodsSpecController();
                var result = await ctrl.MultiSave(new Common.EF.Tables.MultiGoodsSpecSaveForm
                {
                    List = AJAutoMapperService.Instance()
                    .Mapper.Map<List<GoodsSpec>, List<Common.EF.Tables.GoodsSpec>>(Rows.ToList())
                });

                RequestClose(true);
            }
            catch (Exception e)
            {
                ErrorInfo.Show(e.Message);
            }
            finally
            {
                Loading = false;
            }
        }

        public void Add()
        {
            Rows.Add(new GoodsSpec
            {
                Id = YitIdHelper.NextId(),
                GoodsId = (_goods?.Id).GetValueOrDefault(),
                Name = $"{I18NService.Translate(LangKeys.规格)}{Rows.Count + 1}"
            });
            CanCommit = true;
        }

        #region 行选择功能 --阿吉 2024年4月25日10点43分

        private CheckBox _allRowCheckbox;
        private bool _allRowCheckboxProcess;

        private bool _canDeleteSelected;
        public bool CanDeleteSelected
        {
            get
            {
                _canDeleteSelected = Rows.Any(p => p.Checked);
                return _canDeleteSelected;
            }
            set { SetAndNotify(ref _canDeleteSelected, value); }
        }


        public void DeleteSelected()
        {
            var selected = Rows.Where(p => p.Checked).ToList();

            if (MessageBox.Show(I18NService.Translate(LangKeys.确认删除_0_条数据吗, selected.Count), I18NService.Translate(LangKeys.提示), MessageBoxButton.OKCancel)
                == MessageBoxResult.OK)
            {
                Rows.RemoveRange(selected);
                CanCommit = Rows.Count > 0;
            }
        }

        public void RowCheckedChangedHandle(object sender, System.Windows.RoutedEventArgs e)
        {
            if (_allRowCheckboxProcess)
            {
                return;
            }
            var cbx = ((CheckBox)sender);
            var view = cbx.DataContext as GoodsSpec;
            if (view == null)
            {
                return;
            }
            view.Checked = cbx.IsChecked.GetValueOrDefault();

            var checkedCount = 0;

            foreach (var row in Rows)
            {
                if (row.Checked)
                {
                    checkedCount++;
                }
            }

            _allRowCheckbox.IsThreeState = checkedCount > 0 && checkedCount != Rows.Count;
            if (_allRowCheckbox.IsThreeState)
            {
                _allRowCheckbox.IsChecked = null;
            }
            else
            {
                _allRowCheckbox.IsChecked = checkedCount == Rows.Count;
            }
            CanDeleteSelected = Rows.Any(p => p.Checked);
        }


        public void AllRowCheckBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            _allRowCheckbox = (CheckBox)sender;
        }

        public void AllRowCheckedChangedHandle(object sender, System.Windows.RoutedEventArgs e)
        {
            _allRowCheckboxProcess = true;
            var cbx = ((CheckBox)sender);
            cbx.IsThreeState = !cbx.IsChecked.HasValue;
            if (cbx.IsChecked != null && cbx.IsChecked.HasValue)
            {
                foreach (var row in Rows)
                {
                    row.Checked = cbx.IsChecked.GetValueOrDefault();
                }
            }
            _allRowCheckboxProcess = false;
            CanDeleteSelected = Rows.Any(p => p.Checked);
        }



        #endregion

    }
}
